package bsc.war.form;

import bsc.ejb.entity.Person;
import bsc.ejb.entity.PersonScoreDomain;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.entity.ScoreDomain;
import bsc.ejb.entity.ScoreView;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author memozac
 */
public class NewScoreDomain extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");


            String name = request.getParameter("domainName");
            String[] persons = request.getParameterValues("person");
            //String[] typePersons = request.getParameterValues("typePerson");
            Long idMap = request.getParameter("map") != null && NumberUtils.isNumber(request.getParameter("map")) ? Long.parseLong(request.getParameter("map")) : null;

            ScoreCard scoreCard = delegate.findScoreCard(idMap);


            boolean edit = false;
            Long idScoreDomain = -1L;

            if (NumberUtils.isDigits(request.getParameter("idScoreDomain"))) {
                Long id = Long.valueOf(request.getParameter("idScoreDomain"));
                if (id >= 0) {
                    edit = true;
                    idScoreDomain = id;
                }
            }
            ScoreDomain scoreDomain = null;
            ScoreView scoreView = null;

            if (edit) {
                request.setAttribute("edit", Boolean.TRUE);
                scoreDomain = delegate.findScoreDomain(idScoreDomain);
                scoreView = scoreDomain.getScoreView();
            } else {

                scoreDomain = (ScoreDomain) Factory.getBean("ScoreDomain");
                scoreView = (ScoreView) Factory.getBean("ScoreView");
            }

            
            scoreDomain.setName(name);

            

            //Create ScoreView
            scoreView.setName(name);
            scoreView.setDescription(name);
            scoreView.setScoreCard(scoreCard);
            
            if(edit){
                delegate.updateScoreDomain(scoreDomain);
                delegate.updateScoreView(scoreView);
            }else{
                delegate.createScoreDomain(scoreDomain);
                delegate.createScoreView(scoreView);
            }

            scoreView.setScoreDomain(scoreDomain);
            scoreDomain.setScoreView(scoreView);

            delegate.updateScoreDomain(scoreDomain);
            delegate.updateScoreView(scoreView);


            //remove persons
            List<Person> personsDomain = delegate.findPerson(scoreDomain);
            for (Person person : personsDomain) {
                PersonScoreDomain personScoreDomain = delegate.findPersonScoreDomain(person, scoreDomain);
                
                delegate.deletePersonScoreDomain(personScoreDomain);
            }
            
            if (persons != null && persons.length >= 0 && scoreView != null && name != null) {

                for (String idPerson : persons) {
                    PersonScoreDomain personScoreDomain = (PersonScoreDomain) Factory.getBean("PersonScoreDomain");
                    Person person = delegate.findPerson(Long.parseLong(idPerson));
                    personScoreDomain.setPerson(person);
                    personScoreDomain.setDefaultDomain(false);
                    personScoreDomain.setScoreDomain(scoreDomain);
                    String typePerson = request.getParameter("typePerson-"+idPerson);
                    //System.out.println("Type PersonScoreDomain: " + typePerson);
                    personScoreDomain.setProfile(PersonScoreDomain.Profile.valueOf(typePerson));
                    delegate.createPersonScoreDomain(personScoreDomain);
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Caught exception: " + e);
        }

        return new ModelAndView("newScoreDomain");
    }
}
